'use strict';
var events = require('events');
var fs = require('fs');
var http = require('http');
var url = require('url');
var config = require('../../common/config');
var mime = require('../../common/mime');

//web server
http.createServer(function(request, response){
	//解析请求，包括文件名
	var pathname = url.parse(request.url).pathname;///index.html
	//console.log(config.TPLPATH+pathname.substr(1));
	//读文件
	var data = '';
	var readStream = fs.createReadStream(config.TPLPATH+pathname.substr(1));
	readStream.setEncoding('UTF8');
	readStream.on('data', function(chunk){//正在读
		data += chunk;
	})
	readStream.on('end', function(){//读取完毕
		response.writeHead(200, {'Content-Type': 'text/html'});//200 ok
		response.write( data.toString() );//响应文件内容
		response.end();//发送响应数据
	})
	readStream.on('error', function(err){//异常
		response.writeHead(404, {'Content-Type': 'text/html'});//404 error
		response.end();//发送响应数据
	});
	
}).listen( config.PORT );

console.log('Server Start at port:' + config.PORT);